package com.shxy.rlzy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shxy.rlzy.common.lang.Result;
import com.shxy.rlzy.entity.Role;
import com.shxy.rlzy.entity.UserRole;
import com.shxy.rlzy.service.IRoleMenuService;
import com.shxy.rlzy.service.IRoleService;
import com.shxy.rlzy.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tanhao
 * @since 2021-08-16
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    private IRoleService roleService;
    private IRoleMenuService roleMenuService;
    private IUserRoleService userRoleService;

    @Autowired
    public void setUserRoleService(IUserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }

    @Autowired
    public void setRoleMenuService(IRoleMenuService roleMenuService) {
        this.roleMenuService = roleMenuService;
    }

    @Autowired
    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 添加角色
     * @return
     */
    @GetMapping("/add")
    @PreAuthorize("hasAuthority('sys:role:save')")
    public Result add() {
        return Result.succ();
    }

    /**
     * 查询角色列表
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result list() {
        return Result.succ(roleService.list());
    }

    /**
     * 编辑角色信息
     * @return
     */
    @GetMapping("/edit")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result edit() {
        return Result.succ();
    }

    /**
     * 添加角色信息
     * @return
     */
    @PostMapping("/save-update")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result save(@RequestBody Role roleInfo) {
        try {
            // 如果是创建，则添加创建时间
            if(roleInfo.getCreated() == null) {
                roleInfo.setCreated(LocalDateTime.now());
            }
            roleInfo.setUpdated(LocalDateTime.now());   // 修改更新时间
            roleService.saveOrUpdate(roleInfo); // 保存角色信息
            roleMenuService.initRoleMenus(roleInfo.getId());    // 初始化角色菜单信息
            return Result.succ("保存角色成功");
        }
        catch (DuplicateKeyException e){
            return Result.fail("角色名称和代码重复！");
        }
    }

    /**
     * 删除角色信息
     * @return
     */
    @DeleteMapping("/delete/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result delete(@PathVariable Long roleId) {
        // 如果有用户使用该角色，则返回错误
        if(userRoleService.count(new QueryWrapper<UserRole>().eq("role_id", roleId)) > 0){
            return Result.fail("请先删除使用该角色的用户");
        }

        // 删除角色
        roleService.removeById(roleId);

        return Result.succ("删除角色成功!");
    }
}
